
package com.kanjian.music.adapter;

import java.util.ArrayList;
import java.util.Arrays;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.adapter.MusicListListAdapater.ViewHolder;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.volley.VolleyQueue;

public class MusicianListListAdapater extends BaseAdapter {

    private BaseActivity mActivity;

    private LayoutInflater mInflater;

    private ArrayList<Musician> mDataList;

    public MusicianListListAdapater(BaseActivity context, ListView listView) {
        this.mActivity = context;
        mInflater = LayoutInflater.from(mActivity);
        registerOnItemClickListener(listView);
    }

    private void registerOnItemClickListener(ListView listView) {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Musician musician = getItem(position);
                if (musician != null) {
                    Intent intent = new Intent();
                    intent.putExtra(IntentConstants.MUSICIANID, musician.mUserId);
                    intent.setClass(mActivity, MusicianHomeActivity.class);
                    mActivity.startActivityWithAnimation(intent);
                }
            }
        });
    }

    public MusicianListListAdapater(BaseActivity context, ArrayList<Musician> mMusicianList,
            ListView listView) {
        this(context, listView);
        this.mDataList = mMusicianList;
    }

    public void setMusicList(ArrayList<Musician> musicianList) {
        if (mDataList == null) {
            mDataList = new ArrayList<Musician>(musicianList.size());
        } else {
            mDataList.clear();
        }
        mDataList.addAll(musicianList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDataList != null ? mDataList.size() : 0;
    }

    @Override
    public Musician getItem(int position) {
        return mDataList != null ? mDataList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_list_musicianlist, null);
            holder.mIv_musicianPic = (NetworkImageView) convertView
                    .findViewById(R.id.item_list_musicianlist_pic);
            holder.mTv_musicianName = (TextView) convertView
                    .findViewById(R.id.item_list_musicianlist_musicname);
            holder.mTv_musicianGenre = (TextView) convertView
                    .findViewById(R.id.item_list_musicianlist_genre);
            holder.mTv_musicianLocation = (TextView) convertView
                    .findViewById(R.id.item_list_musicianlist_location);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Musician musician = getItem(position);
        holder.mIv_musicianPic.setImageUrl(musician.mUserPic, VolleyQueue.getImageLoader());
        holder.mTv_musicianName.setText(musician.mUserName);
        holder.mTv_musicianGenre.setText(musician.mNewSong.getGenreString());
        holder.mTv_musicianLocation.setText(musician.mUserLocation);
        return convertView;
    }

    static class ViewHolder {

        NetworkImageView mIv_musicianPic;

        TextView mTv_musicianName;

        TextView mTv_musicianGenre;
        
        TextView mTv_musicianLocation;
    }

}
